package org.dromara.pill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.pill.domain.PillFactory;
import org.dromara.pill.domain.bo.PillFactoryBo;
import org.dromara.pill.domain.vo.PillFactoryVo;
import org.dromara.pill.mapper.PillFactoryMapper;
import org.dromara.pill.service.IPillFactoryService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 生产厂家信息Service业务层处理
 *
 * @author Lion Li
 * @date 2023-07-06
 */
@RequiredArgsConstructor
@Service
public class PillFactoryServiceImpl implements IPillFactoryService {

    private final PillFactoryMapper baseMapper;

    /**
     * 查询生产厂家信息
     */
    @Override
    public PillFactoryVo queryById(Long factoryId){
        return baseMapper.selectVoById(factoryId);
    }

    /**
     * 查询生产厂家信息列表
     */
    @Override
    public TableDataInfo<PillFactoryVo> queryPageList(PillFactoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PillFactory> lqw = buildQueryWrapper(bo);
        Page<PillFactoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询生产厂家信息列表
     */
    @Override
    public List<PillFactoryVo> queryList(PillFactoryBo bo) {
        LambdaQueryWrapper<PillFactory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PillFactory> buildQueryWrapper(PillFactoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PillFactory> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getFactoryName()), PillFactory::getFactoryName, bo.getFactoryName());
        lqw.eq(StringUtils.isNotBlank(bo.getFactoryCode()), PillFactory::getFactoryCode, bo.getFactoryCode());
        lqw.eq(StringUtils.isNotBlank(bo.getContact()), PillFactory::getContact, bo.getContact());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), PillFactory::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getKeyword()), PillFactory::getKeyword, bo.getKeyword());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), PillFactory::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增生产厂家信息
     */
    @Override
    public Boolean insertByBo(PillFactoryBo bo) {
        PillFactory add = MapstructUtils.convert(bo, PillFactory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setFactoryId(add.getFactoryId());
        }
        return flag;
    }

    /**
     * 修改生产厂家信息
     */
    @Override
    public Boolean updateByBo(PillFactoryBo bo) {
        PillFactory update = MapstructUtils.convert(bo, PillFactory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PillFactory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除生产厂家信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
